package swing.swing05.model;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.math.BigDecimal;
import java.util.List;

public class ListModelTest {
    JFrame jf=new JFrame("测试ListModel");

    JList<BigDecimal> jList = new JList<>(new NumberListModel(new BigDecimal(1),new BigDecimal(21),new BigDecimal(2)));

    JComboBox<BigDecimal> jComboBox = new JComboBox<>(new NumberComboBoxListModel(new BigDecimal(0.1),new BigDecimal(1.2),new BigDecimal(0.1)));

    JLabel label=new JLabel("您的选择的值是：");
    JTextField jTextField=new JTextField(15);

    public void init(){

        // 组装视图

        // 组装顶部

        // 组装JList
        // 可视高度为4
        jList.setVisibleRowCount(4);
        // 默认选中范围
        jList.setSelectionInterval(2,4);
        // 设置条目的宽高
        jList.setFixedCellWidth(90);
        jList.setFixedCellHeight(30);

        jList.addListSelectionListener(new ListSelectionListener (){
            @Override
            public void valueChanged(ListSelectionEvent e){
                List<BigDecimal> selectedValuesList=jList.getSelectedValuesList();
                jTextField.setText("");
                for(BigDecimal item:selectedValuesList){
                    jTextField.setText(jTextField.getText()+item.toString()+",");
                }
            }
        });


        // 组装JComboBox
        // 设置可是高度为4
        jComboBox.setMaximumRowCount(4);
        jComboBox.addItemListener(new ItemListener(){
            @Override
            public void itemStateChanged(ItemEvent e){
                Object selectedItem=jComboBox.getSelectedItem();
                jTextField.setText(selectedItem.toString());
            }
        });

        Box hBox=Box.createHorizontalBox();
        hBox.add(new JScrollPane(jList));

        JPanel tempPanel=new JPanel();
        tempPanel.add(jComboBox);
        hBox.add(tempPanel);

        jf.add(hBox);

        // 组装底部
        JPanel bottomPanel=new JPanel();
        bottomPanel.add(label);
        bottomPanel.add(jTextField);

        jf.add(bottomPanel, BorderLayout.SOUTH);

        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

    public static void main(String[] args){
        ListModelTest list=new ListModelTest();
        list.init();
    }

}